Return a dynamic string from std::exception's `what`
        Posted  
        
            by Shtééf
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Shtééf
        
        
        
        Published on 2010-04-10T16:43:39Z
        Indexed on 
            2010/04/10
            16:53 UTC
        
        
        Read the original article
        Hit count: 394
        
I'm convinced at this point that I should be creating subclasses of std::exception for all my exception throwing needs. Now I'm looking at how to override the what method.
The situation that I'm facing, it would be really handy if the string what returns be dynamic. Some pieces of code parse an XML file for example, and adding a position or line number to the error message is useful to me.
What I'd like to know:
- whatreturns a- const char *, which implies any catcher is likely not going to free the string. So I need some other place to store the result, but where would that be? (I need thread-safety.)
- whatalso includes- throw()in its signature. While I can prevent my- whatfrom throwing anything, it seems to me that this method really isn't intended for anything too dynamic. If- whatis not the right place, then where should I be doing this instead?
© Stack Overflow or respective owner